#include <unistd.h>
class Solution {
public:
    int dx[4]={0,0,-1,1};
    int dy[4]={1,-1,0,0};

    void dfs(vector<vector<char> >& grid,vector<vector<int> >& visited,int i,int j)
    {
        if(grid[i][j]=='0' || visited[i][j]==1)
        {
            return;
        }
        visited[i][j]=1;
        for(int k=0;k<4;k++)
        {
            int x=i+dx[k];
            int y=j+dy[k];
            if(x>=0 && x<grid.size() && y>=0 && y<grid[0].size())
            dfs(grid,visited,x,y);
        }
    }

    int solve(vector<vector<char> >& grid) {
        int n=0;
        vector<vector<int> > visited(grid.size(),vector<int>(grid[0].size()));
        for(int i=0;i<grid.size();i++)
        {
            for(int j=0;j<grid[0].size();j++)
            {
                if(grid[i][j]=='1' && visited[i][j]==0)
                {
                    dfs(grid,visited,i,j);
                    n++;
                }
            }
        }
        return n;
    }
};